﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;

namespace Web.Infrastructure
{
    public class CustomValidationProvider : DataAnnotationsModelValidatorProvider
    {
        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
        {
            if (metadata.ContainerType != null && metadata.ModelType.Name == "String")
            {
                var attribute = attributes.OfType<StringLengthAttribute>().FirstOrDefault();
                if (attribute == null)
                    attributes = attributes.Concat(new List<Attribute>() { new StringLengthAttribute(256) });
            }
            return base.GetValidators(metadata, context, attributes);
        }
    }
}